﻿using FastReport.DevComponents.DotNetBar.Controls;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Windows.Forms.VisualStyles;
using WinFormsApp1.Common;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.Button;
using static WinFormsApp1.TongShiWei.AddCommodityData_;
using static WinFormsApp1.TongShiWei.ShowIPQCData;
using Button = System.Windows.Forms.Button;

namespace WinFormsApp1.TongShiWei
{
    public partial class AddCommodityData_ : UIForm
    {
        public delegate void GoUseData();
        public event GoUseData UseData;
        //主按钮数据
        List<UIButton> buttons_1 = new List<UIButton>();
        //主按钮数据
        List<UIButton> buttons_2 = new List<UIButton>();
        int IPQCId_ = 0;
        public AddCommodityData_(int IPQCId)
        {
            InitializeComponent();
            //给树形添加数据
            ShowTreeViewData();
            //给事务添加方法
            UseData += ShowPigWeighData_NowData;
            UseData += SHowByproduct_Now;
            UseData += SHowDivision_Now;
            //获取检测标准ID
            IPQCId_ = IPQCId;
        }
        #region 给树形添加数据
        public async void ShowTreeViewData()
        {
            //全部分类
            TreeNode rootNode1 = new TreeNode("全部分类");
            uiTreeView1.Nodes.Add(rootNode1);
            //白条 
            TreeNode rootNode3 = new TreeNode("白条");
            uiTreeView1.Nodes.Add(rootNode3);
            //副产品
            TreeNode rootNode4 = new TreeNode("副产品");
            uiTreeView1.Nodes.Add(rootNode4);
            //分割品
            TreeNode rootNode5 = new TreeNode("分割品");
            uiTreeView1.Nodes.Add(rootNode5);
            //包装物
            TreeNode rootNode6 = new TreeNode("包装物");
            uiTreeView1.Nodes.Add(rootNode6);
        }
        #endregion

        private void AddCommodityData_Load(object sender, EventArgs e)
        {

        }
        //商品分类点击
        int NumType = 0;
        private void uiTreeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            this.dataGridView1.Rows.Clear();
            var GoodsData = e.Node;
            switch (GoodsData.Text)
            {
                case "白条":
                    NumType = 1;
                    ShowPigWeighData_NowData();
                    break;
                case "全部分类":
                    NumType = 0;
                    UseData();
                    break;
                case "副产品":
                    NumType = 2;
                    SHowByproduct_Now();
                    break;
                case "分割品":
                    NumType = 3;
                    SHowDivision_Now();
                    break;
                case "包装物":
                    break;
            };
        }

        #region 商品分类区分
        #region 白条
        List<PigWeighData_Now> pigWeighData_Nows = new List<PigWeighData_Now>()
        {
            new PigWeighData_Now{ PigWeighDataId=1,UPCDataName="精白条",HelpNumber="1001",GeneralName="优白条",GoodsName="白条",UPCTypeName="白条",Brand="乡村黑猪",Origin="河南",Size="大猪",Normbag="无",Unit="公斤",GoodsExplain="无"},
            new PigWeighData_Now{ PigWeighDataId=2,UPCDataName="毛白条",HelpNumber="1002",GeneralName="毛白条",GoodsName="白条",UPCTypeName="白条",Brand="放养黑猪",Origin="东北 ",Size="中猪",Normbag="无",Unit="公斤",GoodsExplain="无"},
            new PigWeighData_Now{ PigWeighDataId=3,UPCDataName="带蹄精白条",HelpNumber="1003",GeneralName="蹄白条",GoodsName="白条",UPCTypeName="白条",Brand="你家黑猪",Origin="河北",Size="大猪",Normbag="无",Unit="公斤",GoodsExplain="无"},
            new PigWeighData_Now{ PigWeighDataId=4,UPCDataName="去蹄精白条",HelpNumber="1004",GeneralName="蹄白条",GoodsName="白条",UPCTypeName="白条",Brand="他家黑猪",Origin="河南",Size="小猪",Normbag="无",Unit="公斤",GoodsExplain="无"},
            new PigWeighData_Now{ PigWeighDataId=5,UPCDataName="肚杂白条",HelpNumber="1005",GeneralName="内白条",GoodsName="白条",UPCTypeName="白条",Brand="我家黑猪",Origin="河南",Size="大猪",Normbag="无",Unit="公斤",GoodsExplain="无"},
        };
        public async void ShowPigWeighData_NowData()
        {
            if (IsAdd > 0)
            {
                for (int i = 0; i < IPQCGoodsId.Count; i++)
                {
                    var data = pigWeighData_Nows.FirstOrDefault(x => x.PigWeighDataId == IPQCGoodsId[i]&& x.GoodsName == IPQCGoodsName[i]);
                    if (data != null)
                    {
                        pigWeighData_Nows.Remove(data);
                    }
                }
            }
            List<PigWeighData_Now> pigWeighData_Nows_ = pigWeighData_Nows;
            if (this.uiTextBox1.Text != "")
            {
                pigWeighData_Nows_ = pigWeighData_Nows_.Where(x => x.UPCDataName.Contains(this.uiTextBox1.Text)).ToList();
                foreach (var item in pigWeighData_Nows_)
                {
                    this.dataGridView1.Rows.Add(false, item.PigWeighDataId, item.UPCDataName, item.HelpNumber, item.GeneralName, item.GoodsName, item.UPCTypeName, item.Brand, item.Origin, item.Size, item.Normbag, item.Unit, item.GoodsExplain);
                };
            }
            else
            {
                foreach (var item in pigWeighData_Nows_)
                {
                    this.dataGridView1.Rows.Add(false, item.PigWeighDataId, item.UPCDataName, item.HelpNumber, item.GeneralName, item.GoodsName, item.UPCTypeName, item.Brand, item.Origin, item.Size, item.Normbag, item.Unit, item.GoodsExplain);
                };
            }
        }
        public class PigWeighData_Now
        {
            //白条ID
            public int PigWeighDataId { get; set; }
            //商品代号
            public string UPCDataName { get; set; }
            //助记码
            public string HelpNumber { get; set; }
            //通用名称
            public string GeneralName { get; set; }
            //商品名称
            public string GoodsName { get; set; }
            //代号分类
            public string UPCTypeName { get; set; }
            //品牌
            public string Brand { get; set; }
            //产地
            public string Origin { get; set; }
            //规格
            public string Size { get; set; }
            //标包
            public string Normbag { get; set; }
            //计算单位
            public string Unit { get; set; }
            //商品说明
            public string GoodsExplain { get; set; }
        }
        #endregion
        #region 副产品
        List<Byproduct_Now> Byproduct_Nows = new List<Byproduct_Now>()
            {
                new Byproduct_Now{ ByproductId=1,UPCDataName="猪心",HelpNumber="1001",GeneralName="心",GoodsName="副产",UPCTypeName="副产",Brand="乡村黑猪",Origin="河南",Size="大猪",Normbag="无",Unit="公斤",GoodsExplain="无"},
                new Byproduct_Now{ ByproductId=2,UPCDataName="猪肝",HelpNumber="1002",GeneralName="肝",GoodsName="副产",UPCTypeName="副产",Brand="放养黑猪",Origin="东北 ",Size="中猪",Normbag="无",Unit="公斤",GoodsExplain="无"},
                new Byproduct_Now{ ByproductId=3,UPCDataName="猪肺",HelpNumber="1003",GeneralName="肺",GoodsName="副产",UPCTypeName="副产",Brand="你家黑猪",Origin="河北",Size="大猪",Normbag="无",Unit="公斤",GoodsExplain="无"}
            };
        public async void SHowByproduct_Now()
        {
            if (IsAdd > 0)
            {
                for (int i = 0; i < IPQCGoodsId.Count; i++)
                {
                    var data = Byproduct_Nows.FirstOrDefault(x => x.ByproductId == IPQCGoodsId[i] && x.GoodsName == IPQCGoodsName[i]);
                    if (data != null)
                    {
                        Byproduct_Nows.Remove(data);
                    }
                }
            }
            List<Byproduct_Now> Byproduct_Nows_ = Byproduct_Nows;
            if (this.uiTextBox1.Text != "")
            {
                Byproduct_Nows_ = Byproduct_Nows.Where(x => x.UPCDataName.Contains(this.uiTextBox1.Text)).ToList();
                foreach (var item in Byproduct_Nows_)
                {
                    this.dataGridView1.Rows.Add(false, item.ByproductId, item.UPCDataName, item.HelpNumber, item.GeneralName, item.GoodsName, item.UPCTypeName, item.Brand, item.Origin, item.Size, item.Normbag, item.Unit, item.GoodsExplain);
                };
            }
            else
            {
                foreach (var item in Byproduct_Nows_)
                {
                    this.dataGridView1.Rows.Add(false, item.ByproductId, item.UPCDataName, item.HelpNumber, item.GeneralName, item.GoodsName, item.UPCTypeName, item.Brand, item.Origin, item.Size, item.Normbag, item.Unit, item.GoodsExplain);
                };
            }
        }
        public class Byproduct_Now
        {
            //白条ID
            public int ByproductId { get; set; }
            //商品代号
            public string UPCDataName { get; set; }
            //助记码
            public string HelpNumber { get; set; }
            //通用名称
            public string GeneralName { get; set; }
            //商品名称
            public string GoodsName { get; set; }
            //代号分类
            public string UPCTypeName { get; set; }
            //品牌
            public string Brand { get; set; }
            //产地
            public string Origin { get; set; }
            //规格
            public string Size { get; set; }
            //标包
            public string Normbag { get; set; }
            //计算单位
            public string Unit { get; set; }
            //商品说明
            public string GoodsExplain { get; set; }
        }
        #endregion
        #region 分割品
        List<Division_Now> Divisiont_Nows = new List<Division_Now>()
        {
            new Division_Now{ DivisionId=1,UPCDataName="猪肚",HelpNumber="1001",GeneralName="心",GoodsName="分割",UPCTypeName="分割",Brand="乡村黑猪",Origin="河南",Size="大猪",Normbag="无",Unit="公斤",GoodsExplain="无"},
            new Division_Now{ DivisionId=2,UPCDataName="猪肺",HelpNumber="1002",GeneralName="肝",GoodsName="分割",UPCTypeName="分割",Brand="放养黑猪",Origin="东北 ",Size="中猪",Normbag="无",Unit="公斤",GoodsExplain="无"}
        };
        public async void SHowDivision_Now()
        {
            if (IsAdd > 0)
            {
                for (int i = 0; i < IPQCGoodsId.Count; i++)
                {
                    var data = Divisiont_Nows.FirstOrDefault(x => x.DivisionId == IPQCGoodsId[i] && x.GoodsName == IPQCGoodsName[i]);
                    if (data != null)
                    {
                        Divisiont_Nows.Remove(data);
                    }
                }
                IsAdd = 0;
                IPQCGoodsId = new List<int>();
                IPQCGoodsName = new List<string>();
            }
            List<Division_Now> Divisiont_Nows_ = Divisiont_Nows;
            if (this.uiTextBox1.Text != "")
            {
                Divisiont_Nows_ = Divisiont_Nows.Where(x => x.UPCDataName.Contains(this.uiTextBox1.Text)).ToList();
                foreach (var item in Divisiont_Nows_)
                {
                    this.dataGridView1.Rows.Add(false, item.DivisionId, item.UPCDataName, item.HelpNumber, item.GeneralName, item.GoodsName, item.UPCTypeName, item.Brand, item.Origin, item.Size, item.Normbag, item.Unit, item.GoodsExplain);
                };
            }
            else
            {
                foreach (var item in Divisiont_Nows_)
                {
                    this.dataGridView1.Rows.Add(false, item.DivisionId, item.UPCDataName, item.HelpNumber, item.GeneralName, item.GoodsName, item.UPCTypeName, item.Brand, item.Origin, item.Size, item.Normbag, item.Unit, item.GoodsExplain);
                };
            }
        }
        public class Division_Now
        {
            //白条ID
            public int DivisionId { get; set; }
            //商品代号
            public string UPCDataName { get; set; }
            //助记码
            public string HelpNumber { get; set; }
            //通用名称
            public string GeneralName { get; set; }
            //商品名称
            public string GoodsName { get; set; }
            //代号分类
            public string UPCTypeName { get; set; }
            //品牌
            public string Brand { get; set; }
            //产地
            public string Origin { get; set; }
            //规格
            public string Size { get; set; }
            //标包
            public string Normbag { get; set; }
            //计算单位
            public string Unit { get; set; }
            //商品说明
            public string GoodsExplain { get; set; }
        }
        #endregion
        #region 包装物
        #endregion
        #endregion
        //关闭当前页面
        private void uiButton3_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        //获取表格中选中的多选框
        List<Button> buttons = new List<Button>();
        List<Button> button2 = new List<Button>();
        //添加商品ID
        List<int> IPQCGoodsId=new List<int>();
        //添加商品名称
        List<string> IPQCGoodsName=new List<string>();
        public async void UpDataGriViewChecked()
        {
            //主按钮
            int x1 = 4; int y1 = 773; int i = 0;
            int x2 = 110; int y2 = 773;
            foreach (DataGridViewRow item in this.dataGridView1.Rows)
            {
                if (((bool)this.dataGridView1.Rows[item.Index].Cells[0].Value)&& item.Index>-1)
                {

                    Button button = new Button();
                    button.Location = new System.Drawing.Point(x2, y2);
                    button.Text = "×";
                    button.Name = this.dataGridView1.Rows[item.Index].Cells[2].Value.ToString();
                    button.Size = new Size(25, 30);
                    button.ForeColor = Color.Black;
                    button.Click += new System.EventHandler(DelCheckedGoods);
                    //button.AutoSize = true;
                    this.Controls.Add(button);
                    button2.Add(button);
                    x2 += 140;
                    //主按钮
                    Button uIButton = new Button();
                    uIButton.Location = new System.Drawing.Point(x1, y1);
                    uIButton.Text = this.dataGridView1.Rows[item.Index].Cells[2].Value.ToString();
                    uIButton.Size = new Size(130, 52);
                    uIButton.ForeColor = Color.Black;
                    //uIButton.TextAlign = ContentAlignment.MiddleCenter;
                    uIButton.AutoSize = true;
                    this.Controls.Add(uIButton);
                    buttons.Add(uIButton);
                    x1 += 140;
                    //添加商品ID
                    IPQCGoodsId.Add(Convert.ToInt32(this.dataGridView1.Rows[item.Index].Cells[1].Value));
                    //添加商品名称
                    IPQCGoodsName.Add(this.dataGridView1.Rows[item.Index].Cells[5].Value.ToString());
                }
            }
        }
        //取消选中商品
        public async void DelCheckedGoods(object sender, EventArgs a)
        {
            var BUttonData = sender as Button;
            foreach (DataGridViewRow item in this.dataGridView1.Rows)
            {
                if (BUttonData.Name == (item.Cells[2].Value.ToString()))
                {
                    item.Cells[0].Value = false;
                }
            }
            foreach (var item in buttons)
            {
                this.Controls.Remove(item);
            }
            buttons.Clear();
            foreach (var item in button2)
            {
                this.Controls.Remove(item);
            }
            button2.Clear();
            UpDataGriViewChecked();
        }
        //全选
        private void uiButton1_Click(object sender, EventArgs e)
        {
            foreach (DataGridViewRow item in dataGridView1.Rows)
            {
                this.dataGridView1.Rows[item.Index].Cells[0].Value = true;
            }
            IPQCGoodsId = new List<int>();
            IPQCGoodsName= new List<string>();
            UpDataGriViewChecked();
        }
        //单选
        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex > 0)
            {
                return;
            }
            bool CheckData = (bool)this.dataGridView1.Rows[e.RowIndex].Cells[0].Value;
            CheckData = CheckData == true ? false : true;
            this.dataGridView1.Rows[e.RowIndex].Cells[0].Value = CheckData;
            IPQCGoodsId = new List<int>();
            IPQCGoodsName = new List<string>();
            UpDataGriViewChecked();
        }
        private void uiButton2_Click(object sender, EventArgs e)
        {
            this.dataGridView1.Rows.Clear();
            switch (NumType)
            {
                case 1:
                    ShowPigWeighData_NowData();
                    break;
                case 0:
                    UseData();
                    break;
                case 2:
                    SHowByproduct_Now();
                    break;
                case 3:
                    NumType = 4;
                    SHowDivision_Now();
                    break;
            }
        }
        #region 添加检测标准应用商品
        int IsAdd = 0;
        private async void AddPRQSButton_Click(object sender, EventArgs e)
        {
            //实例化应用商品
            IPQCGoodsData_ data = new IPQCGoodsData_() 
            {
                IPQCGoodsId=0,
                IPQCId=IPQCId_,
                GoodsId=IPQCGoodsId,
                GoodsName=IPQCGoodsName,
                IsiPQCGoods=false
            };
            //http://localhost:5159/api/TSW/AddIPQCGoodsData
            var res = await HttpClientHelper.RequestUrl<IPQCGoodsData_, ApiResult<IPQCGoodsData_>>(URLCommon.Url1, "/TSW/AddIPQCGoodsData", HttpClientHelper.MethodType.POST, data);
            if (res.Code > 0)
            {
                MessageBox.Show("添加成功");
                this.dataGridView1.Rows.Clear();
                foreach (var item in buttons)
                {
                    this.Controls.Remove(item);
                }
                buttons.Clear();
                foreach (var item in button2)
                {
                    this.Controls.Remove(item);
                }
                button2.Clear();
                IsAdd = 1;
                UseData();
            }
            else
            {
                MessageBox.Show("添加失败");
            }
        }
        public class IPQCGoodsData_
        {
            /// <summary>
            /// 检疫标准商品缓存表Id
            /// </summary>
            public int IPQCGoodsId { get; set; }
            /// <summary>
            /// 检验标准表ID
            /// </summary>
            public int IPQCId { get; set; }
            /// <summary>
            /// 检疫商品表ID
            /// </summary>
            public List<int> GoodsId { get; set; }
            /// <summary>
            /// 检验商品名称
            /// </summary>
            public List<string> GoodsName { get; set; }
            /// <summary>
            /// 是否删除
            /// </summary>
            public bool IsiPQCGoods { get; set; }
        }
        #endregion
    }
}
